/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.dp1.pucp.Compras.VIsta;


import com.dp1.pucp.Modelo.Clases.Proveedor;
import com.dp1.pucp.Util.Validaciones;
import java.awt.event.ActionListener;
import java.awt.event.ItemListener;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author tupapi.com
 */
public class MantProveedorGUI extends javax.swing.JPanel {

    /**
     * Creates new form BuscarUsuarioGUI
     */
    private JFrame padre;
    //private ProveedorControlador c;

    
    public MantProveedorGUI() {
        initComponents();
    }
      
    public JFrame getPadre(){ return padre; }
    
    public String getNombre(){ return txtRazon.getText(); }  
           
    public int getFilaSel(){ return tblResultados.getSelectedRow(); }  
    
     public int getEstado(){ return cmbEstado.getSelectedIndex(); } 

 /*   public void mostrarAviso(String msn){
        Validaciones.mostrarMensajeAvisoVal(msn);   
    }
    public void mostrarError(String msn){
        Validaciones.mostrarMensajeErrorVal(msn);           
    }
    public int solicitarConfirmacion(String msn){
        return Validaciones.solicitarConfirmacionVal(msn);
    }    */
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        txtRazon = new javax.swing.JTextField();
        btnLimpiar = new javax.swing.JButton();
        btnBuscar = new javax.swing.JButton();
        btnCrear = new javax.swing.JButton();
        btnEditar = new javax.swing.JButton();
        btnEliminar = new javax.swing.JButton();
        btnAsignarProd = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        txtRUC = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        cmbEstado = new javax.swing.JComboBox();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblResultados = new javax.swing.JTable();

        setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
        setMaximumSize(new java.awt.Dimension(683, 663));
        setMinimumSize(new java.awt.Dimension(683, 663));
        setPreferredSize(new java.awt.Dimension(683, 663));

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Buscar", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Ubuntu", 0, 15))); // NOI18N

        jLabel1.setText("Razon Social");

        txtRazon.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtRazonKeyTyped(evt);
            }
        });

        btnLimpiar.setFont(new java.awt.Font("Ubuntu", 0, 10)); // NOI18N
        btnLimpiar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/clear.png"))); // NOI18N
        btnLimpiar.setText("Limpiar");
        btnLimpiar.setBorderPainted(false);
        btnLimpiar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnLimpiar.setVerticalAlignment(javax.swing.SwingConstants.BOTTOM);
        btnLimpiar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);

        btnBuscar.setFont(new java.awt.Font("Ubuntu", 0, 10)); // NOI18N
        btnBuscar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/buscar.png"))); // NOI18N
        btnBuscar.setText("Buscar");
        btnBuscar.setBorderPainted(false);
        btnBuscar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnBuscar.setVerticalAlignment(javax.swing.SwingConstants.BOTTOM);
        btnBuscar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);

        btnCrear.setFont(new java.awt.Font("Ubuntu", 0, 10)); // NOI18N
        btnCrear.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/nuevo.png"))); // NOI18N
        btnCrear.setText("Nuevo");
        btnCrear.setBorderPainted(false);
        btnCrear.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnCrear.setVerticalAlignment(javax.swing.SwingConstants.BOTTOM);
        btnCrear.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);

        btnEditar.setFont(new java.awt.Font("Ubuntu", 0, 10)); // NOI18N
        btnEditar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/edit1.png"))); // NOI18N
        btnEditar.setText("Editar");
        btnEditar.setBorderPainted(false);
        btnEditar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnEditar.setMaximumSize(new java.awt.Dimension(57, 29));
        btnEditar.setMinimumSize(new java.awt.Dimension(57, 29));
        btnEditar.setPreferredSize(new java.awt.Dimension(57, 29));
        btnEditar.setVerticalAlignment(javax.swing.SwingConstants.BOTTOM);
        btnEditar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);

        btnEliminar.setFont(new java.awt.Font("Ubuntu", 0, 10)); // NOI18N
        btnEliminar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/delete2.png"))); // NOI18N
        btnEliminar.setText("Inactivar");
        btnEliminar.setBorderPainted(false);
        btnEliminar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnEliminar.setMaximumSize(new java.awt.Dimension(57, 29));
        btnEliminar.setMinimumSize(new java.awt.Dimension(57, 29));
        btnEliminar.setPreferredSize(new java.awt.Dimension(57, 29));
        btnEliminar.setVerticalAlignment(javax.swing.SwingConstants.BOTTOM);
        btnEliminar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);

        btnAsignarProd.setFont(new java.awt.Font("Ubuntu", 0, 10)); // NOI18N
        btnAsignarProd.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/edit2.png"))); // NOI18N
        btnAsignarProd.setText("Productos");
        btnAsignarProd.setBorderPainted(false);
        btnAsignarProd.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnAsignarProd.setMaximumSize(new java.awt.Dimension(57, 29));
        btnAsignarProd.setMinimumSize(new java.awt.Dimension(57, 29));
        btnAsignarProd.setPreferredSize(new java.awt.Dimension(57, 29));
        btnAsignarProd.setVerticalAlignment(javax.swing.SwingConstants.BOTTOM);
        btnAsignarProd.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnAsignarProd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAsignarProdActionPerformed(evt);
            }
        });

        jLabel4.setText("RUC");

        txtRUC.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtRUCKeyTyped(evt);
            }
        });

        jLabel2.setText("Estado");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(btnCrear, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnEditar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnEliminar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnAsignarProd, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(263, 263, 263))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addGap(150, 150, 150)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtRazon)
                    .addComponent(txtRUC)
                    .addComponent(cmbEstado, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(150, 150, 150))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(464, 464, 464)
                .addComponent(btnBuscar, javax.swing.GroupLayout.DEFAULT_SIZE, 92, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnLimpiar, javax.swing.GroupLayout.DEFAULT_SIZE, 91, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnEliminar, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnAsignarProd, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnEditar, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnCrear, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(34, 34, 34)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtRUC, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtRazon, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cmbEstado, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnLimpiar)
                    .addComponent(btnBuscar))
                .addContainerGap())
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Resultados", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Ubuntu", 0, 15))); // NOI18N

        tblResultados.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "NOMBRE", "RUC", "CONTACTO", "TELEFONO", "UBICACION", "CATEGORIA", "ESTADO"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tblResultados.getTableHeader().setResizingAllowed(false);
        tblResultados.getTableHeader().setReorderingAllowed(false);
        jScrollPane1.setViewportView(tblResultados);
        tblResultados.getColumnModel().getColumn(6).setResizable(false);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 236, Short.MAX_VALUE)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnAsignarProdActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAsignarProdActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_btnAsignarProdActionPerformed

    private void txtRUCKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtRUCKeyTyped
        // TODO add your handling code here:
        String car = txtRUC.getText();
        if(car.length()>=11) evt.consume();        
        Validaciones.soloDigito(evt);
    }//GEN-LAST:event_txtRUCKeyTyped

    private void txtRazonKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtRazonKeyTyped
        // TODO add your handling code here:
         String car = txtRazon.getText();
        if(car.length()>=70) evt.consume();    
        Validaciones.soloDigitoTextoPuntuacion(evt);
    }//GEN-LAST:event_txtRazonKeyTyped

    public void addNuevoListener(ActionListener ml){ this.btnCrear.addActionListener(ml); }
    
    public void addBuscarListener(ActionListener ml){ this.btnBuscar.addActionListener(ml); }    

    public void addLimpiarListener(ActionListener ml){ this.btnLimpiar.addActionListener(ml); }  
  
    public void addEditarListener(ActionListener ml){ this.btnEditar.addActionListener(ml); } 
    
    public void addEliminarListener(ActionListener ml){ this.btnEliminar.addActionListener(ml); } 
    
    public void addAsignarProductoListener(ActionListener ml){ this.btnAsignarProd.addActionListener(ml); } 

    public void realizarFiltro(ArrayList<Proveedor> lista) {
       ((DefaultTableModel) this.tblResultados.getModel()).setRowCount(0);
        ArrayList<Proveedor> listOrden = new ArrayList<>();
        String ruc = txtRUC.getText();
        String razonSocial = txtRazon.getText();

        if (lista.size()==0){ //Esto es si es q en la base de datos no hay ninguna orden guardada
            mostrarAviso("No se ha registrado ningún Proveedor");
        }
        else{
            //veo si es que puso algun codigo, si es que si solo mostrará una orden unica
            if (!(ruc.equals(""))){
                for (int i = 0; i < lista.size(); i++) {
                    if (ruc.equals(lista.get(i).getRuc())) {
                    listOrden.add(lista.get(i));
                    }
                }
            }
            else{
                //Para comprobar con a razon social 
                
                if (this.getEstado() == 0) {
                    if (!(razonSocial.equals(""))){
                       for (int i = 0; i < lista.size(); i++) {
                        if (razonSocial.toLowerCase().contains(lista.get(i).getRazonsocial().toLowerCase())) {
                        listOrden.add(lista.get(i));
                         }
                     }
                    this.llenaGrilla(listOrden);
                    return;
                    }
                    else {
                    this.llenaGrilla(lista);
                    return;
                    }
                }
                
                if (!(ruc.equals(""))){
                for (int i = 0; i < lista.size(); i++) {
                    if ((this.getEstado()-1 == lista.get(i).getEstado())& (razonSocial.toLowerCase().contains(lista.get(i).getRazonsocial().toLowerCase()))) {
                    listOrden.add(lista.get(i));
                    }
                }
                }
                else{
                   for (int i = 0; i < lista.size(); i++) {
                    if (this.getEstado()-1 == lista.get(i).getEstado()) {
                    listOrden.add(lista.get(i));
                    }
                } 
                }
            
            }
        

        this.llenaGrilla(listOrden);
        
        }
        return;
    }
    
    public void llenaGrilla(ArrayList<Proveedor> lista){        
        if(lista!=null){
             if (lista.size()==0) {
                 mostrarAviso("No se encuentra ningun Proveedor bajo este criterio");}
            else{
            ((DefaultTableModel) this.tblResultados.getModel()).setRowCount(0);
            for (int i = 0; i<lista.size();i++){
                ((DefaultTableModel) tblResultados.getModel()).setRowCount(tblResultados.getRowCount()+1);
                tblResultados.setValueAt(lista.get(i).getRazonsocial(),i,0);
                tblResultados.setValueAt(lista.get(i).getRuc(),i,1);
                tblResultados.setValueAt(lista.get(i).getContacto(),i,2);
                tblResultados.setValueAt(lista.get(i).getTelefonocontacto(),i,3);
                tblResultados.setValueAt(lista.get(i).getDireccionfiscal(),i,4);
                if(lista.get(i).getCategoria()==1){
                    tblResultados.setValueAt("Alto",i,5);
                }
                if(lista.get(i).getCategoria() ==2){
                    tblResultados.setValueAt("Medio",i,5);
                }
                if(lista.get(i).getCategoria() ==3){
                    tblResultados.setValueAt("Bajo",i,5);
                }
                
                if(lista.get(i).getEstado() ==1){
                    tblResultados.setValueAt("Activo",i,6);
                }
                if(lista.get(i).getEstado() ==0){
                    tblResultados.setValueAt("Inactivo",i,6);
                }
//                if(lista.get(i).getEstado() ==3){
//                    tblResultados.setValueAt("Inhabilitado",i,6);
//                }
             }
             }
        }
    }    
              
    public void llenaComboEstado(ArrayList<String> lista){
        if(lista!=null){
            for (String opc : lista){
                cmbEstado.addItem(opc);
            }
        }
    }    
    
    public void LimpiarDatos(){
        txtRazon.setText("");
        ((DefaultTableModel) this.tblResultados.getModel()).setRowCount(0);
    }
    
    public void mostrarAviso(String msn){
        Validaciones.mostrarMensajeAvisoVal(msn);   
    }
    
    public void mostrarError(String msn){
        Validaciones.mostrarMensajeErrorVal(msn);           
    }
    public int solicitarConfirmacion(String msn){
        return Validaciones.solicitarConfirmacionVal(msn);
    }   
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAsignarProd;
    private javax.swing.JButton btnBuscar;
    private javax.swing.JButton btnCrear;
    private javax.swing.JButton btnEditar;
    private javax.swing.JButton btnEliminar;
    private javax.swing.JButton btnLimpiar;
    private javax.swing.JComboBox cmbEstado;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tblResultados;
    private javax.swing.JTextField txtRUC;
    private javax.swing.JTextField txtRazon;
    // End of variables declaration//GEN-END:variables
}
